package com.just.sxmiaosha.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.just.sxmiaosha.cache.StockCache;
import com.just.sxmiaosha.dao.StockDao;
import com.just.sxmiaosha.entity.Stock;
import com.just.sxmiaosha.service.StockService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author just
 * @since 2020-09-23
 */
@Service
@Slf4j
public class StockServiceImpl implements StockService {


    @Autowired
    private StockDao stockDao;

    @Autowired
    private StockCache stockCache;

    @Override
    public Stock getStockById(int id) {
        Stock stock = stockDao.getStockCountById(id);
        log.info("打印查询:{}", stock.toString());
        return stock;
    }


    @Override
    public int getStockCountById(int id) {
        Stock stock = stockDao.getStockCountById(id);
        if (stock != null) {
            return stock.getCount() - stock.getSale();
        }
        return 0;
    }

    @Override
    public int getStockCountCache(int id) {

        int countCache = stockCache.getStockCountCache(id);
        if (countCache == 0) {
            countCache = getStockCountById(id);
            log.info("获取到的缓存库存{}", countCache);
            stockCache.setStockCountCache(id, countCache);
        }
        return countCache;
    }


    public void delStockCountCache(int id) {
        stockCache.delStockCountCache(id);
    }
}
